package com.example.test.controller;

import com.example.test.bean.DTO.UserAddressDTO;
import com.example.test.bean.domain.UserAddress;
import com.example.test.common.ResponseHelper;
import com.example.test.service.UserAddressService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/user/address")
@Validated
@Tag(name = "用户地址管理")
public class UserAddressController {

	@Resource
	private UserAddressService userAddressService;

	@Operation(summary = "新增地址")
	@PostMapping("/add")
	public ResponseHelper<?> addAddress(@Valid @RequestBody UserAddressDTO addressDTO) {
		userAddressService.addAddress(addressDTO);
		return ResponseHelper.genResponse();
	}

	@Operation(summary = "修改地址")
	@PutMapping("/update")
	public ResponseHelper<?> updateAddress(@Valid @RequestBody UserAddressDTO addressDTO) {
		userAddressService.updateAddress(addressDTO);
		return ResponseHelper.genResponse();
	}

	@Operation(summary = "删除地址")
	@DeleteMapping("/delete")
	public ResponseHelper<?> deleteAddress(@RequestParam Integer id, @RequestParam Integer userId) {
		userAddressService.deleteAddress(id, userId);
		return ResponseHelper.genResponse();
	}

	@Operation(summary = "查询用户地址")
	@GetMapping("/get")
	public ResponseHelper<List<UserAddress>> getAddressById(@RequestParam Integer userId) {
		List<UserAddress> vo = userAddressService.getAddressById(userId);
		return ResponseHelper.genResponse(vo);
	}
}